/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.canvaschart.client.xy;

import java.util.Iterator;
import java.util.LinkedList;

import net.kornr.canvaschart.client.core.Point;

public class DefaultXYDataSet implements XYDataSet 
{
	private LinkedList<Point> m_data = new LinkedList<Point>();
	
	public void add(double x, double y)
	{
		Point p = new Point(x,y);
		m_data.add(p);
	}
	
	public Point get(int offset)
	{
		return m_data.get(offset);
	}
	
	public double getX(int offset) {
		// TODO Auto-generated method stub
		return m_data.get(offset).getX();
	}

	public double getY(int offset) {
		// TODO Auto-generated method stub
		return m_data.get(offset).getY();
	}

	public int size() {
		// TODO Auto-generated method stub
		return m_data.size();
	}

	public Point getNearestFromX(double x)
	{
		Point best = null;
		double distance = Double.MAX_VALUE;
		for (Iterator<Point> i = m_data.iterator(); i.hasNext(); )
		{
			Point p = i.next();
			double dist = Math.abs(p.getX() - x);
			if (dist < distance)
			{
				distance = dist;
				best = p;
			}
		}
		return best;
	}
	
}
